package api

import (
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func ListPack(c *gin.Context) {
	var req request.LoginReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	pack, err := c1.ListPack(c, &__.ListPackRequest{})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": pack,
	})

}

func WeightPack(c *gin.Context) {

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	pack, err := c1.WeightPack(c, &__.WeightPackRequest{})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": pack,
	})

}

func SortPack(c *gin.Context) {

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	pack, err := c1.WeightSortPack(c, &__.WeightSortPackRequest{})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": pack,
	})

}

func PutSms(c *gin.Context) {
	var req request.PutSmsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	pack, err := c1.PutSms(c, &__.PutSmsRequest{
		PackagesId: req.PackagesId,
		Source:     req.Source,
	})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": pack,
	})
}

func PutPack(c *gin.Context) {
	var req request.PutPackReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	pack, err := c1.PutPack(c, &__.PutPackRequest{
		PackagesId: req.PackagesId,
		PutSms:     req.PutSms,
	})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": pack,
	})
}
